//
//  SEUploadAPI.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/12.
//

import Moya

enum SEUploadAPI {
    case getFileUrl(bucket: String, type: String, relationId: String, isShare: String)
    case insertDBAfterUploadFile(bucket: String,
                                 type: String,
                                 relationId: String,
                                 introduction: String,
                                 size: Double,
                                 url: String,
                                 systemId: Int,
                                 checkStatus: Int,
                                 remarks: String,
                                 isIm: Int)
    case getPresignedUrlForUploadFile(bucket: String)
    case uploadBase64File(pictureFile: String, bucket: String, type: String, relationId: String, systemId: Int, checkStatus: Int)
}

extension SEUploadAPI: TargetType {
    var baseURL: URL {
        // TODO:
        return URL(string: SEApiManager.baseUrl)!
    }
    
    var path: String {
        switch self {
        case .getFileUrl: return "/ng/newfuture-file/file/getFileUrls"
        case .getPresignedUrlForUploadFile: return "/ng/newfuture-file/file/getPresignedUrl"
        case .insertDBAfterUploadFile: return "/ng/newfuture-file/file/insertTbresource"
        case .uploadBase64File: return "/ng/newfuture-file/file/uploadBase64File"
        }
    }
    
    var method: Moya.Method { return .post }
    
    var sampleData: Data { return "".data(using: .utf8)! }
    
    var task: Task {
        switch self {
        case .getFileUrl:
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case .getPresignedUrlForUploadFile, .insertDBAfterUploadFile, .uploadBase64File:
            return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
        }
    }
    
    var headers: [String : String]? { SEApiManager.headers }
    
    private var parameters: [String: Any] {
        switch self {
        case let .getFileUrl(bucket, type, relationId, isShare):
            return ["bucket": bucket, "type": type, "relationId": relationId, "isShare": isShare]
        case let .insertDBAfterUploadFile(bucket, type, relationId, introduction, size, url, systemId, checkStatus, remarks, isIm):
            return [
                "bucket": bucket,
                "type": type,
                "relationId": relationId,
                "introduction": introduction,
                "size": size,
                "url": url,
                "systemId": systemId,
                "checkStatus": checkStatus,
                "remarks": remarks,
                "isIm": isIm
            ]
        case let .getPresignedUrlForUploadFile(bucket):
            return ["bucket": bucket]
        case let .uploadBase64File(pictureFile, bucket, type, relationId, systemId, checkStatus):
            return [
                "pictureFile": pictureFile,
                "bucket": bucket,
                "type": type,
                "relationId": relationId,
                "systemId": systemId,
                "checkStatus": checkStatus
            ]
        }
    }
}
